#include "Engine.h"

using namespace sf;

jfw::Engine::Engine()
{}

bool jfw::Engine::LoadSettings(std::string m_sFilename,std::string m_sSeparator="=")
{
    std::fstream ConfigFile;
    ConfigFile.open(m_sFilename.c_str(), std::ios::in);
    if(ConfigFile.is_open())
    {
        std::string tmp_sLine="";
        int tmp_iSeparatorAt=0;

        while(ConfigFile.good())
            {
                getline(ConfigFile,tmp_sLine);

                tmp_iSeparatorAt = tmp_sLine.find(m_sSeparator);

                Settings.insert(std::pair<std::string,std::string>(tmp_sLine.substr(0,tmp_iSeparatorAt),
                                                                      tmp_sLine.substr(tmp_iSeparatorAt,tmp_sLine.length()-tmp_iSeparatorAt)));

                tmp_sLine="";
                tmp_iSeparatorAt=0;
            }
        ConfigFile.close();
        return true;
    }
    else
    {
        return false;
    }
}

bool jfw::Engine::Open()
{

}

bool jfw::Engine::Open(int iWidth=1024, int iHeight=768, int iBits=32, const char* sTitle="")
{
    //App(VideoMode(iWidth, iHeight, iBits), sTitle/*, Style::Titlebar|Style::Close, WindowSettings(24,8,2)*/);
    App.Create(VideoMode(iWidth, iHeight, iBits), sTitle, Style::Titlebar|Style::Close, WindowSettings(24,8,2));
    return true;
}

void jfw::Engine::Update(float m_fTime)
{
    fElapsedTime += Clock.GetElapsedTime();
    Clock.Reset();

    while(App.GetEvent(Event))
    {
        if (Event.Type == Event::Closed || Event.Key.Code == Key::Escape)
        {
            this->Close();
        }
    }
}

void jfw::Engine::Draw()
{
    App.Clear();

    App.Display();
}

/*bool jfw::Engine::GetIsOpened()
{
    return App.IsOpened();
}
*/
void jfw::Engine::Close()
{
    App.Close();
//    delete App;
}

jfw::Engine::~Engine()
{
}

//jfw::Engine* pEngine = NULL;
